if...else and Nested if...else of C++

03-11-17 Course- CPP

If you want to read C++ then we suggest first you read C programming lanuguage.... then you good understand this tutorial.

The if, if...else and nested if...else statement are used to make one-time decisions in C++ Programming, that is, to execute some code/s and ignore some code/s depending on test condition. Without decision making, the program runs in similar way every time. Decision making is an important feature of all programming language using C++ programming. Before you learn decision making, you should have basic understanding of relational operators.

C++ if Statement

The if statement checks whether the test condition is true or not. If the test condition is true, it executes the code/s inside the body of ifstatement. But it the test condition is false, it skips the code/s inside the body of if statement.

Working of if Statement

Working of if statement in C++ Programming

The if keyword is followed by test condition inside parenthesis ( ). If the test condition is true, the codes inside curly bracket is executed but if test condition is false, the codes inside curly bracket { } is skipped and control of program goes just below the body of if as shown in figure above.

Flowchart of if

Flowchat of if statement in C++ Programming

Example 1: C++ if Statement

C++ Program to print integer entered by user only if that number is positive.


#include <iostream>
using namespace std;

int main() {
    int number;
    cout<< "Enter an integer: ";
    cin>> number;

    if ( number > 0) {  // Checking whether an integer is positive or not.
        cout << "You entered a positive integer: "<<number<<endl;
    }

    cout<<"This statement is always executed because it's outside if statement.";
    return 0;

}

Output 1


Enter an integer: 5
You entered a positive number: 5
This statement is always executed because it's outside if statement.

Output 2


Enter a number: -5
This statement is always executed because it's outside if statement.

C++ if...else

The if...else executes body of if when the test expression is true and executes the body of else if test expression is false.

Working of if...else Statement

Working of if else statement in C++ Programming

The if statement checks whether the test expression is true or not. If the test condition is true, it executes the code/s inside the body of if statement. But it the test condition is false, it executes the code/s inside the body of else.

Flowchart of if...else

Flowchart of if...else statement in C++ Programming

Example 2: C++ if...else Statement

C++ Program to check whether integer entered by user is positive or negative (Considering 0 as positive)


#include <iostream>
using namespace std;

int main() {
    int number;
    cout<< "Enter an integer: ";
    cin>> number;

    if ( number >= 0) {
        cout << "You entered a positive integer: "<<number<<endl;
    }
    
    else {
        cout<<"You entered a negative integer: "<<number<<endl;
    }

    cout<<"This statement is always executed because it's outside if...else statement.";
    return 0;
    
}

Output


Enter an integer: -4
You entered a negative integer: -4
This statement is always executed because it's outside if...else statement.

C++ Nested if...else

Nested if...else are used if there are more than one test expression.

Syntax of Nested if...else


if (test expression1){
     statement/s to be executed if test expression1 is true;
     }
     else if(test expression2) {
          statement/s to be executed if test expression1 is false and 2 is true;
     }
     else if (test expression 3) {
         statement/s to be executed if text expression1 and 2 are false and 3 is true;
     }
         .
         .
         .
     else {
            statements to be executed if all test expressions are false;
       }

The nested if...else statement has more than one test expression. If the first test expression is true, it executes the code inside the braces{ } just below it. But if the first test expression is false, it checks the second test expression. If the second test expression is true, if executes the code inside the braces{ } just below it. This process continues. If all the test expression are false, code/s inside else is executed and the control of program jumps below the nested if...else

Example 3: C++ Nested if...else Statement

C++ Program to check whether the integer entered by user is positive, negative or zero.


#include <iostream>
using namespace std;

int main() {
    int number;
    cout<< "Enter an integer: ";
    cin>> number;

    if ( number > 0) {
        cout << "You entered a positive integer: "<<number<<endl;
    }
    else if (number < 0){
        cout<<"You entered a negative integer: "<<number<<endl;
    }
    else {
        cout<<"You entered 0."<<endl;
    }

    cout<<"This statement is always executed because it's outside nested if..else statement.";
    return 0;
    
}

Output


Enter an integer: 0
You entered 0.
This statement is always executed because it's outside nested if..else statement.

Conditional/Ternary Operator ?:

Conditional operators are the peculiar case of if...else statement in C++ Programming. Consider this if..else statement:


if ( a < b ) {
   a = b;
}
else {
   a = -b;
}

The above code can be written using conditional operator as:


a = (a < b) ? b : -b;

Both codes above check whether a is less than b or not. If a is less than b, value of b is assigned to a if not, -b is assigned to a.